﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmDonvi.
	/// </summary>
	public class frmDonvi : System.Windows.Forms.Form
	{
		private AccessData  mDB=new AccessData();
        Language lan = new Language();
		private DataSet ds=new DataSet();
		private System.Windows.Forms.DataGrid dgrDanhMuc6;
		private System.Windows.Forms.TextBox txtTen;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private MaskedTextBox txtMa;
        private Button btn_them;
        private Button btn_sua;
        private Button btn_luu;
        private Button btn_boqua;
        private Button btn_xoa;
        private Button btn_thoat;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDonvi()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDonvi));
            this.dgrDanhMuc6 = new System.Windows.Forms.DataGrid();
            this.txtTen = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.txtMa = new System.Windows.Forms.MaskedTextBox();
            this.btn_them = new System.Windows.Forms.Button();
            this.btn_sua = new System.Windows.Forms.Button();
            this.btn_luu = new System.Windows.Forms.Button();
            this.btn_boqua = new System.Windows.Forms.Button();
            this.btn_xoa = new System.Windows.Forms.Button();
            this.btn_thoat = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc6)).BeginInit();
            this.SuspendLayout();
            // 
            // dgrDanhMuc6
            // 
            this.dgrDanhMuc6.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrDanhMuc6.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc6.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrDanhMuc6.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrDanhMuc6.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrDanhMuc6.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc6.CaptionVisible = false;
            this.dgrDanhMuc6.DataMember = "";
            this.dgrDanhMuc6.FlatMode = true;
            this.dgrDanhMuc6.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrDanhMuc6.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc6.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc6.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrDanhMuc6.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc6.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrDanhMuc6.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc6.LinkColor = System.Drawing.Color.Teal;
            this.dgrDanhMuc6.Location = new System.Drawing.Point(8, 8);
            this.dgrDanhMuc6.Name = "dgrDanhMuc6";
            this.dgrDanhMuc6.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc6.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc6.RowHeaderWidth = 10;
            this.dgrDanhMuc6.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrDanhMuc6.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc6.Size = new System.Drawing.Size(464, 312);
            this.dgrDanhMuc6.TabIndex = 17;
            this.dgrDanhMuc6.CurrentCellChanged += new System.EventHandler(this.dgrDanhMuc6_CurrentCellChanged);
            // 
            // txtTen
            // 
            this.txtTen.BackColor = System.Drawing.Color.White;
            this.txtTen.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtTen.Location = new System.Drawing.Point(256, 328);
            this.txtTen.MaxLength = 60;
            this.txtTen.Name = "txtTen";
            this.txtTen.Size = new System.Drawing.Size(216, 20);
            this.txtTen.TabIndex = 117;
            this.txtTen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtTen_KeyDown);
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Control;
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(168, 329);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(82, 13);
            this.label2.TabIndex = 116;
            this.label2.Text = "Tên bệnh viện :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.Control;
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(12, 330);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(78, 13);
            this.label1.TabIndex = 114;
            this.label1.Text = "Mã bệnh viện :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtMa
            // 
            this.txtMa.BackColor = System.Drawing.SystemColors.Control;
            this.txtMa.Enabled = false;
            this.txtMa.Location = new System.Drawing.Point(89, 325);
            this.txtMa.Mask = "###.#.##";
            this.txtMa.Name = "txtMa";
            this.txtMa.Size = new System.Drawing.Size(78, 20);
            this.txtMa.TabIndex = 118;
            this.txtMa.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMa_KeyDown);
            // 
            // btn_them
            // 
            this.btn_them.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_them.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_them.Image = global::Human.Properties.Resources.add;
            this.btn_them.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_them.Location = new System.Drawing.Point(25, 363);
            this.btn_them.Name = "btn_them";
            this.btn_them.Size = new System.Drawing.Size(65, 28);
            this.btn_them.TabIndex = 119;
            this.btn_them.Text = "&Thêm";
            this.btn_them.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_them.UseVisualStyleBackColor = true;
            this.btn_them.Click += new System.EventHandler(this.btn_them_Click);
            this.btn_them.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btn_them_KeyDown);
            // 
            // btn_sua
            // 
            this.btn_sua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_sua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_sua.Image = global::Human.Properties.Resources.quick_edit;
            this.btn_sua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_sua.Location = new System.Drawing.Point(96, 363);
            this.btn_sua.Name = "btn_sua";
            this.btn_sua.Size = new System.Drawing.Size(65, 28);
            this.btn_sua.TabIndex = 120;
            this.btn_sua.Text = "  &Sửa";
            this.btn_sua.UseVisualStyleBackColor = true;
            this.btn_sua.Click += new System.EventHandler(this.btn_sua_Click);
            this.btn_sua.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btn_sua_KeyDown);
            // 
            // btn_luu
            // 
            this.btn_luu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_luu.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_luu.Image = global::Human.Properties.Resources.save;
            this.btn_luu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_luu.Location = new System.Drawing.Point(167, 363);
            this.btn_luu.Name = "btn_luu";
            this.btn_luu.Size = new System.Drawing.Size(65, 28);
            this.btn_luu.TabIndex = 121;
            this.btn_luu.Text = "   &Lưu";
            this.btn_luu.UseVisualStyleBackColor = true;
            this.btn_luu.Click += new System.EventHandler(this.btn_luu_Click);
            this.btn_luu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btn_luu_KeyDown);
            // 
            // btn_boqua
            // 
            this.btn_boqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_boqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_boqua.Image = global::Human.Properties.Resources.t_boqua1;
            this.btn_boqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_boqua.Location = new System.Drawing.Point(238, 363);
            this.btn_boqua.Name = "btn_boqua";
            this.btn_boqua.Size = new System.Drawing.Size(70, 28);
            this.btn_boqua.TabIndex = 122;
            this.btn_boqua.Text = "&Bỏ qua";
            this.btn_boqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_boqua.UseVisualStyleBackColor = true;
            this.btn_boqua.Click += new System.EventHandler(this.btn_boqua_Click);
            // 
            // btn_xoa
            // 
            this.btn_xoa.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_xoa.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_xoa.Image = global::Human.Properties.Resources.delete_enabled;
            this.btn_xoa.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_xoa.Location = new System.Drawing.Point(314, 363);
            this.btn_xoa.Name = "btn_xoa";
            this.btn_xoa.Size = new System.Drawing.Size(65, 28);
            this.btn_xoa.TabIndex = 123;
            this.btn_xoa.Text = "   &Xóa";
            this.btn_xoa.UseVisualStyleBackColor = true;
            this.btn_xoa.Click += new System.EventHandler(this.btn_xoa_Click);
            // 
            // btn_thoat
            // 
            this.btn_thoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_thoat.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_thoat.Image = global::Human.Properties.Resources.close_r;
            this.btn_thoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_thoat.Location = new System.Drawing.Point(384, 363);
            this.btn_thoat.Name = "btn_thoat";
            this.btn_thoat.Size = new System.Drawing.Size(65, 28);
            this.btn_thoat.TabIndex = 124;
            this.btn_thoat.Text = "&Thoát";
            this.btn_thoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_thoat.UseVisualStyleBackColor = true;
            this.btn_thoat.Click += new System.EventHandler(this.btn_thoat_Click);
            // 
            // frmDonvi
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(480, 408);
            this.Controls.Add(this.btn_thoat);
            this.Controls.Add(this.btn_xoa);
            this.Controls.Add(this.btn_boqua);
            this.Controls.Add(this.btn_luu);
            this.Controls.Add(this.btn_sua);
            this.Controls.Add(this.btn_them);
            this.Controls.Add(this.txtMa);
            this.Controls.Add(this.txtTen);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dgrDanhMuc6);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmDonvi";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục nơi đăng ký BHYT";
            this.Load += new System.EventHandler(this.frmDonvi_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc6)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmDonvi_Load(object sender, System.EventArgs e)
		{
			donvi();
			Ena_control(true);
			refresh_Text();
			Ena_textbox(false);
			btn_them.Focus();
		}
		
		private void Ena_control(bool ena)
		{
			btn_them.Enabled=ena;
			btn_sua.Enabled=ena;
			btn_luu.Enabled=!ena;
			btn_boqua.Enabled=!ena;
			btn_xoa.Enabled=ena;
			btn_thoat.Enabled=ena;
		}
		private void Ena_textbox(bool ena)
		{	
			txtTen.Enabled=ena;
		}
		private void clear_Text()
		{			
			txtMa.Text ="";
			txtTen.Clear();
		}
		private void refresh_Text()
		{
			int i=dgrDanhMuc6.CurrentRowIndex;
			txtMa.Enabled =false;
			if(i>=0)
			{
				txtMa.Text=dgrDanhMuc6[i,0].ToString();
				txtTen.Text=dgrDanhMuc6[i,1].ToString();
			}
		}	
	
		private void btnKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		private void donvi() 	
		{
			DataSet ds1=new DataSet();
            ds1 = mDB.get_data("SELECT mabv,tenbv,diachi FROM " + mDB.user + ".benhvien order by mabv");
			ds1.Tables[0].TableName="dv";
			dgrDanhMuc6.DataSource=null;
			dgrDanhMuc6.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = "dv";
		
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "mabv";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã bệnh viện");
			TextCol1.Width = 100;
			TextCol1.ReadOnly=true;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc6.TableStyles.Add(ts);
		
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "tenbv";
			TextCol2.HeaderText = lan.Change_language_MessageText("Tên bệnh viện");
			TextCol2.Width = 300;
			TextCol2.ReadOnly=true;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrDanhMuc6.TableStyles.Add(ts);
		 			 
			ts.DataGrid.DataSource=ds1.Tables[0]; 
		}

		private void dgrDanhMuc6_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				refresh_Text();
				Ena_textbox(false);
				Ena_control(true);
			}
			catch{}
		}

		private void txtTen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter)
				btn_luu.Focus();
		}

        private void txtMa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            Ena_control(false);
            txtMa.Enabled = true;
            Ena_textbox(true);
            clear_Text();
            txtMa.Focus();
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            Ena_control(false);
            Ena_textbox(false);
            txtMa.Enabled = false;
            txtTen.Enabled = true;
            txtTen.Focus();
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMa.Text.Trim() != "")
                {
                    mDB.Insert_DMBENHVIEN(txtMa.Text, txtTen.Text);
                    donvi();
                    Ena_control(true);
                    txtMa.Enabled = false;
                    Ena_textbox(false);
                    btn_them.Focus();
                }
            }
            catch { }
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            Ena_control(true);
            txtMa.Enabled = false;
            txtTen.Enabled = false;
            refresh_Text();
            btn_thoat.Focus();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ đơn vị này?"),AccessData.Msg, MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    mDB.get_data("DELETE FROM " + mDB.user + ".benhvien WHERE mabv='" + txtMa.Text.Trim() + "'");
                }
                donvi();
                refresh_Text();
            }
            catch { }
        }

        private void btn_luu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_them.Focus();
        }

        private void btn_them_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMa.Focus();
        }

        private void btn_sua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMa.Focus();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
	}
}
